package com.ideal.gpt.app.local;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.lzwork.utility.file.LineContent;
import com.lzwork.utility.file.LineContent.LoadSetting;
import com.lzwork.utility.str.StrUtility;

/**
 * @author LiZheng
 * @date 2023年11月3日 下午5:25:25
 */

public class ClearFile
{
	public static String normalize(String input)
	{
		if (!StrUtility.stringValid(input))
			return "";

		String str = input.trim();

		String[] lines = str.split("\n");
		List<String> output = new ArrayList<>();

		for (String l : lines)
		{
			String line = l.trim();
			if (!StrUtility.stringValid(line))
				continue;
			output.add(line);
		}

		return StrUtility.combineString(output, "\n");
	}

	public static void clearMark(File input, File output)
	{
		List<String> lines = LineContent.loadFile(input, new LoadSetting());

		List<String> outputList = new ArrayList<>();
		int check = 0;

		for (String l : lines)
		{
			String line = clearCell(l);
			outputList.add(line);
			check++;
		}

		System.out.println("All check " + check);

		LineContent.saveFile(output, outputList);
	}

	public static String clearCell(String input)
	{
		String line = input;
		if (line.startsWith("\""))
			line = line.substring(1);

		line = line.replace("&nbsp;", " ");
		line = line.replace("<p>", "\n");
		line = line.replace("</p>", "\n");

		int begin = line.indexOf('<');

		while (begin >= 0)
		{
			int end = line.indexOf('>', begin);
			if (end < 0)
				break;

			String cut = line.substring(begin, end + 1);
			line = line.replace(cut, " ");

			begin = line.indexOf('<');
		}

		if (line.endsWith("\""))
			line = line.substring(0, line.length() - 1);

		return line;
	}

	public static void main(String[] args)
	{
		File input = new File("v:/12345/qadb_content.csv");
		File output = new File("v:/12345/qadb_content.txt");

		clearMark(input, output);
	}
}
